package com.csyf.manager.controller.system;

import com.csyf.manager.common.CommonConst;
import com.csyf.manager.entity.SysAgentChannel;
import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.entity.SysTradeType;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.service.SysChannelBiz;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.service.SysTradeTypeBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author: casper
 * @Description: 商户管理
 * @Date: Created in 2018/11/28 14:20
 */
@Controller
@RequestMapping("/merch")
public class SysMerchController {

    @Autowired
    private SysMerchBiz sysMerchService;
    @Autowired
    private SysTradeTypeBiz sysTradeTypeBiz;
    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;
    @Autowired
    private SysChannelBiz sysChannelBiz;

    /**
     * 列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public JqGridVo list(SysMerchant vo, Integer page, Integer rows) {
        return sysMerchService.querydata(vo, page, rows);
    }

    /**
     * 新增商户
     */
    @ResponseBody
    @PostMapping(value = "add")
    public ResultEntity add(SysMerchant agentForm) {
        try {
            sysMerchService.saveAgent(agentForm);
            return ResultUtli.DEFAULT_SUCCESS("添加商户成功", null);
        } catch (ApiValidateException e) {
            return ResultUtli.DEFAULT_ERROR("添加商户失败" + e.getMessage());
        }
    }

    /**
     * 编辑用户详情
     * @param model
     * @param merchId
     * @return
     */
    @GetMapping("edit")
    public String editAgent(Model model, String merchId) {
        SysMerchant merch = sysMerchService.queryAgentById(Long.valueOf(merchId));
        model.addAttribute("merch", merch);
        return "merch/merchDetail";
    }

    /**
     * 编辑商户
     * @param agentForm
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResultEntity editAgent(SysMerchant agentForm) {
        int num = sysMerchService.updateByAgentId(agentForm);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("编辑商户成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("编辑商户失败");
        }
    }

    /**
     * 删除商户
     * @param merchId
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public ResultEntity deleteMerch(String merchId) {
        List<String> merchIdIdsList = Arrays.asList(merchId);
        int num = sysMerchService.updateStatusBatch(merchIdIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除商户成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除商户失败");
        }
    }

    /**
     * 批量删除商户
     * @param agentIdStr
     * @return
     */
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResultEntity batchDeleteAgent(String agentIdStr) {
        String[] agentIds = agentIdStr.split(",");
        List<String> agentIdsList = Arrays.asList(agentIds);
        int num = sysMerchService.updateStatusBatch(agentIdsList, CommonConst.STATUS_INVALID);
        if (num > 0) {
            return ResultUtli.DEFAULT_SUCCESS("删除商户成功", null);
        } else {
            return ResultUtli.DEFAULT_ERROR("删除商户失败");
        }
    }


    /**
     * 代付商修改商户费率
     * @param model
     * @param merchId
     * @return
     */
    @GetMapping("merchrate")
    public String merchupdaterate(Model model, String merchId) {
        SysMerchant merch = sysMerchService.queryAgentById(Long.valueOf(merchId));
        model.addAttribute("merch", merch);

        // 查询上游列表
        List<SysAgentChannel> agentChannels = sysChannelBiz.queryByMchId("");
        model.addAttribute("channels", agentChannels);
        return "merch/merchrate";
    }


    /**
     * 代付商修改商户费率
     * @param tradeType
     * @return
     */
    @PostMapping("/merchrate")
    @ResponseBody
    public ResultEntity merchrate(SysTradeType tradeType) {

        //校验业务类型是否存在
        boolean isexist = sysChannelAttrBiz.isExistChannel(tradeType.getTradeType());
        if (!isexist) {
            return ResultUtli.ERRO(10, "该业务类型不存在，请重试！", null);
        }
        //校验业务类型是否重复
        boolean isexisttrade = sysTradeTypeBiz.isExist(tradeType);
        if (isexisttrade) {
            sysTradeTypeBiz.addTradeType(tradeType);
            return ResultUtli.DEFAULT_SUCCESS("成功", null);
        }
        sysTradeTypeBiz.addTradeType(tradeType);

        return ResultUtli.DEFAULT_SUCCESS("成功", null);

    }

    @PostMapping("/getAll")
    @ResponseBody
    public ResultEntity queryAllAgent() {
        List<SysMerchant> merchs = sysMerchService.queryAll();

        Map<Long,String> merchMap = new TreeMap<>();
        for (SysMerchant merch : merchs) {
            merchMap.put(merch.getId(), merch.getMerchName());
        }

        return ResultUtli.DEFAULT_SUCCESS("查询商户信息成功", merchMap);
    }


}
